
use "${source_data}\Basis_Labor_Data.dta", clear

drop if dataset=="Production"

preserve
keep b_id 
drop if b_id==.
duplicates drop 
sort b_id 
* 226 buyers
save "${replicated_data}\Sample_b_PD.dta", replace
restore 

clonevar S_sourcing = S_sourcing_metric_b_excl
clonevar D_sourcing = D_sourcing_metric_b_excl

label var D_sourcing "\$Relational^D_b\$" 
label var S_sourcing "\$Relational_b\$" 

label var lnNw "\$\# Workers_{slb\tau}\$"
label var Nw "\$\# Workers_{slb\tau}\$"
label var E "\$Efficency_{slb\tau}\$"
label var EFF "\$Efficency_{slb\tau}\$"
label var shH "\$Share \ Helpers_{slb\tau}\$"
label var output_target "\$Target_{slb\tau}\$"
label var ln_runtime_h "\$Runtime_{slb\tau}\$"
label var runtime_h "\$Runtime_{slb\tau}\$"
label var smv "\$SMV_{slb\tau}\$"

* ------------------------------------ *
* DESCRIPTIVES *
* ------------------------------------ *

distinct factory_code line_code date if dataset=="Shocks"
distinct factory_code line_code date if dataset=="Female II"

bys line_code: egen n_days_l=nvals(date)
bys line_code: gen n_l=_n

sum n_days_l if n_l==1 & dataset=="Shocks"
sum n_days_l if n_l==1 & dataset=="Female II"

tab dataset 
tab dataset if b_id!=.

codebook date if  dataset=="Shocks"
codebook date if  dataset=="Female II"

* ----------------------------------------------------------------------------- *
* TABLE B5. Summary Statistics of Production, Workers, and HR variables (In Sample)
* ----------------------------------------------------------------------------- *

do "${floats}\TableB5PanelA.do"

* ------------------------------------ *
* REGRESSIONS *
* ------------------------------------ *
capture drop line
egen line=group(factory_code line_code)

capture drop SAMPLE
gen SAMPLE=(S_sourcing!=.)

gen data_fem=(dataset=="Female II")

label var SAMPLE "\$In \ Sample\$"

* ----------------------------------------------------------------------------- *
* TABLE B3. Observations with and without buyer characteristics - Production Line Data
* ----------------------------------------------------------------------------- *

do "${floats}\TableB3.do"

* ----------------------------------------------------------------------------- *
* TABLE 5. Buyers' Sourcing and Labor Usage
* ----------------------------------------------------------------------------- *

do "${floats}\Table5.do"

* ----------------------------------------------------------------------------- *
* TABLE C14. Buyers' Sourcing and Runtime
* ----------------------------------------------------------------------------- *

do "${floats}\TableC14.do"

* ----------------------------------------------------------------------------- *
* TABLE D2. Buyers' Sourcing and Labor Usage: Alternative Time Horizons
* ----------------------------------------------------------------------------- *

label var has_quality_records "\$Record_{slb\tau}\$"
label var reject_rate "\$Reject_{slb\tau}\$"
label var defect_rate "\$Defect_{slb\tau}\$"

do "${floats}\TableD2.do"  
  
* ------------------------------------ *
* ANALYSIS OF SURVEY DATA *
* ------------------------------------ *
  
use "${source_data}\Survey_Analysis_Data.dta", clear

capture drop constant
gen constant=1

gen secondary_plus=(EDUCATION>=4)
clonevar GENDER_original=GENDER
drop GENDER

gen GENDER=0
replace GENDER=1 if (GENDER_original==1 & survey_data=="Female II") | (GENDER_original==2 & survey_data!="Female II") 
gen experience=ln(EXP_GARMENT_MONTHS+1)
  
label var GENDER "\$Female_{isl}\$"
label var AGE "\$Age_{isl}\$"
label var secondary_plus "\$Educated_{isl}\$"
label var EXP_GARMENT_MONTHS "\$Experience_{isl}\$"
label var experience "\$Experience_{isl}\$"
label var markraven "\$Ability_{isl}\$"

label var ln_wage "\$Wage_{isl}\$"
label var sh_r_l "\$Relational_{sl}\$"
label var PAY_PIECE_RATE "\$Piece \ Rate_{isl}\$"
label var PAY_TARGET_BONUS "\$Target_{isl}\$"
label var PAY_QUALITY_BONUS "\$Quality_{isl}\$"
label var PAY_EFFICIENY_BONUS "\$Efficiency_{isl}\$"
label var PAY_OTHER_BONUS "\$Other_{isl}\$"

* ----------------------------------------------------------------------------- *
* TABLE C3. Workers' Wages, Bonuses and Demographics
* ----------------------------------------------------------------------------- *

do "${floats}\TableC3.do"  

* ----------------------------------------------------------------------------- *
* TABLE B5. Summary Statistics of Production, Workers, and HR variables (In Sample)
* ----------------------------------------------------------------------------- *

do "${floats}\TableB5PanelB1.do" 
do "${floats}\TableB5PanelB2.do"   
  
* ----------------------------------------------------------------------------- *
* TABLE B4. Observations with and without buyer characteristics - Survey Data
* ----------------------------------------------------------------------------- *

do "${floats}\TableB4.do"   
  
* ------------------------------------ *
* ANALYSIS OF HR DATA *
* ------------------------------------ *
  
use "${source_data}\Basis_Labor_Data.dta", clear
drop if dataset=="Production"
drop if dataset=="Shocks"

gen xx=(D_sourcing_metric_b_excl!=.)
gen yy=(D_sourcing_metric_b_excl==1)

bys factory_code month: egen n_ld_f=total(xx)
bys factory_code month: egen n_ldr_f=total(yy)
gen sh_r_fm=n_ldr_f/n_ld_f

keep sh_r* factory_code line_code month dataset
sort line_code month
duplicates drop
isid line_code month

di _N 
distinct line_code 
distinct line_code if sh_r_lm!=. 

clonevar orig_line_code=line_code
destring line_code, force replace

clonevar orig_factory_code=factory_code
destring factory_code, force replace
rename factory_code fact_code

keep sh_r_fm fact_code month
duplicates drop 

sort fact_code month
merge 1:m fact_code month using "${source_data}\HR_Data.dta"

drop if _merge!=3 
count if sh_r_fm!=.
egen wid=group(fact_code id) 
egen FE_line=group(line_code) 
bys wid: egen num_lines_w=nvals(line_code) 
bys wid: gen n_w=_n

tab num_lines_w if n_w==1

gen quality_designation=0
local N 8 9 10 17 18 31 32 33 34 67 43 70
foreach n of local N{
	replace quality_designation=1 if desig_clean==`n'	
}
gen downstream_designation=0
local N 35 36 37 38 39 40 41 42 26
foreach n of local N{
	replace downstream_designation=1 if desig_clean==`n'	
}
gen upstream_designation=0
local N 47 48 49 50 53 54 56 66
foreach n of local N{
	replace upstream_designation=1 if desig_clean==`n'	
}
gen manager_designation=0
local N 1 19 20 21 22 23 24 25 26 27 28 30 44 45 46
foreach n of local N{
	replace manager_designation=1 if desig_clean==`n'	
}

gen general_designation=(quality_designation==1 | downstream_designation==1 | upstream_designation==1  )

capture drop overtime_hours 
replace ot_hrs=0 if ot_hrs==.
replace eot_hrs=0 if eot_hrs==.
gen overtime_hours=(ot_hrs + eot_hrs)
gen lnovertime_hours=ln(overtime_hours+1)

gen lnnetpay=ln(netpay)
gen lnbasic=ln(basic)
gen lngrosspay=ln(grosspay)

egen FE_factory=group(fact_code)
egen FE_factmonth=group(fact_code month)

replace absentdays=0 if absentdays==.
gen lnabsentdays=ln(absentdays+1)

label var attendance "\$Attendace_{ism}\$"
label var absentdays "\$Absent_{ism}\$"
label var lnabsentdays "\$Absent_{ism}\$"
label var overtime_hours "\$Overtime_{ism}\$"
label var lnovertime_hours "\$Overtime_{ism}\$"
label var lnbasic "\$Wage_{ism}\$"
label var lnnetpay "\$Wage_{ism}\$"
label var lngrosspay "\$Wage_{ism}\$"
label var sh_r_fm "\$Relational_{sm}\$"


* ----------------------------------------------------------------------------- *
* TABLE C13. Overtime and Pay when Producing for Relational Buyers
* ----------------------------------------------------------------------------- *

do "${floats}\TableC13.do"
 
* ----------------------------------------------------------------------------- *
* TABLE B5. Summary Statistics of Production, Workers, and HR variables (In Sample)
* ----------------------------------------------------------------------------- *

do "${floats}\TableB5PanelC.do"

* ----------------------------------------------------------------------------- *
* TABLE B2. Coverage
* ----------------------------------------------------------------------------- *
  
use "${source_data}\FII_ALL_Surveys_l.dta", clear
distinct uid line_code fact_cod

do "${floats}\TableB2.do"

use "${source_data}\ST_ALL_Surveys_l.dta", clear
distinct uid line_code fact_cod

do "${floats}\TableB2.do"
